package user;

import service.DB;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class SignIn extends javax.servlet.http.HttpServlet implements ServletContextAttributeListener{
  protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    DB db = DB.getDb();
    PrintWriter out = response.getWriter();
    try {
      String uid = request.getParameter("uid");
      if (db.signIn(uid, request.getParameter("pw")).equals(1)) {
        HttpSession session = request.getSession();
        session.setAttribute("uid", uid);
        session.setAttribute("user", new User());
        out.print("login success");
      } else {
        out.print("login failed");
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    ServletContext context = this.getServletContext();
    context.setAttribute("test", "test");
    System.out.println("test");
  }

  public void attributeAdded(ServletContextAttributeEvent sae) {
    System.out.println("tttttttttttttt");
    System.out.println("name" + sae.getName());
    System.out.println("value" + sae.getValue());
  }
}
